<?php
/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/7/14
 * Time: 下午9:25
 */

class BuildBaseInfoService extends BaseService
{
    public $accountId;

    public function __construct()
    {
        parent::__construct();
//        $this->accountId = session('account_id');
    }
    /**
     * 通过园区id获取园区信息
     * @param int $commId 园区id
     * @return array
     */
    public function getCommunityInfo($commId)
    {
        if (!$commId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "community_$commId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }
        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'community_id' => $commId,
            'cut_off' => 0
        );
        $data = D('Community')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个楼栋的信息
     * @param int $buildId 楼栋id
     * @return array
     */
    public function getBuildInfo($buildId)
    {
        if (!$buildId || !$this->accountId) {
            return null;
        }

        //先从缓存中去获取
        $cacheKey = "building_{$buildId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'building_id' => $buildId,
            'cut_off' => 0
        );
        $data = D('Building')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个楼层的信息
     * @param int $floorId 楼层id
     * @return mixed
     */
    public function getFloorInfo($floorId)
    {
        if (!$floorId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "floor_$floorId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'floor_id' => $floorId,
            'cut_off' => 0
        );
        $data = D('Floor')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个寝室的信息
     * @param int $roomId 寝室id
     * @return mixed
     */
    public function getRoomInfo($roomId)
    {
        if (!$roomId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "room_$roomId";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'room_id' => $roomId,
            'cut_off' => 0
        );
        $data = D('Room')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取所有园区列表的信息
     * @param
     * @return array
     */
    public function getCommunityList()
    {
        if (!$this->accountId) {
            return null;
        }

        $cids = D('Community')->where(array('account_id'=>$this->accountId))->getField('community_id',true);
        if (!$cids) {
            return null;
        }

        $newCommunityList = array();
        foreach ($cids as $cid) {
            $newCommunityList[] = $this->getCommunityInfo($cid);
        }

        return $newCommunityList;
    }

    /**
     * 通过园区id获得楼幢列表信息
     * @param int $commId 园区id
     * @return array|null
     */
    public function getBuildListByCommId($commId)
    {
        if (!$commId) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'community_id' => $commId,
            'cut_off' => 0
        );
        $bids = D('Building')->where($condition)->getField('building_id',true);
        if (!$bids) {
            return null;
        }

        $newBuildList = array();
        foreach ($bids as $bid) {
            $newBuildList[] = $this->getBuildInfo($bid);
        }
        return $newBuildList;
    }

    /**
     * 通过楼幢id获取当前楼幢的楼层列表信息
     * @param int $building_id 楼幢id
     * @return array
     */
    public function getFloorListByBuildingId($building_id){
        if (!$building_id) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'building_id' => $building_id,
            'cut_off' => 0
        );
        $floorIdArr = D('Floor')->where($condition)->getField('floor_id',true);
        if (!$floorIdArr) {
            return null;
        }

        $newFloorList = array();
        foreach ($floorIdArr as $fid) {
            $newFloorList[] = $this->getFloorInfo($fid);
        }
        return $newFloorList;
    }

    /**
     * 通过楼层id获取当前楼层的房间列表信息
     * @$param int $floor_id 楼层id
     * @return array
     */
    public function getRoomListByFloorId($floor_id){
        if (!$floor_id) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'floor_id' => $floor_id,
            'cut_off' => 0
        );
        $rids = D('Room')->where($condition)->getField('room_id',true);
        if (!$rids) {
            return null;
        }
        $newRoomList = array();
        foreach ($rids as $rid) {
            $newRoomList[] = $this->getRoomInfo($rid);
        }

        return $newRoomList;
    }
}